﻿using Microsoft.Extensions.DependencyInjection;
using SBlogCore.Common.CacheExtend.MemoryCacheExtension;
using SBlogCore.Common.CacheExtend.RedisCacheExtension;

namespace SBlogCore.Common.CacheExtend
{
    public static class SBlogCacheExtensions
    {
        public static IServiceCollection AddSBlogCatchSetUp(this IServiceCollection services)
        {
            //默认只开启一个缓存
            //是否开启Redis缓存
            string enableRedis = AppSetting.ReadAppSettings("Cache:Redis:Enable");
            //是否开启本地缓存
            //string enableMemoryCache = AppSetting.ReadAppSettings("Cache:MemoryCache:Enable");
            if (!string.IsNullOrEmpty(enableRedis) && bool.Parse(enableRedis))
            {
                #region old
                //Nuget Microsoft.Extensions.Caching.StackExchangeRedis
                //services.AddStackExchangeRedisCache(options => {
                //    options.Configuration = configuration;
                //    options.InstanceName = instanceName;//所有的缓存键值前面都会加上redis_
                //}); 
                #endregion
                services.AddScoped<ISBlogCoreCache, SBlogRedisCache>();
            }
            else 
            {
                //本地缓存
                services.AddMemoryCache();
                services.AddScoped<ISBlogCoreCache, SBlogMemoryCache>();
            }
            return services;
        }
    }
}
